import com.greyfit.model.Program
import com.greyfit.model.ProgramLifts
import com.greyfit.security.GreyFitUser
import com.greyfit.security.Role
import com.greyfit.security.User
import com.greyfit.security.UserRole



class BootStrap {

    def init = { servletContext ->
        def adminRole = Role.findByAuthority('ROLE_ADMIN') ?: new Role(authority: 'ROLE_ADMIN').save(failOnError: true)
        def userRole = Role.findByAuthority('ROLE_USER') ?: new Role(authority: 'ROLE_USER').save(failOnError: true)

        def user1 = GreyFitUser.findByUsername('bobby@example.org') ?: new GreyFitUser(username: 'bobby@example.org', enabled: true, password: 'pass', firstName: 'Bobby', lastName: 'Warner', phone: '1111111111').save(failOnError: true)
        if (!user1.authorities.contains(userRole)) {
            UserRole.create user1, userRole, true
        }

        def user2 = GreyFitUser.findByUsername('admin@example.org') ?: new GreyFitUser(username: 'admin@example.org', enabled: true, password: 'pass', firstName: 'Admin', lastName: 'Admin', phone: '2222222222').save(failOnError: true)
        if (!user2.authorities.contains(userRole)) {
            UserRole.create user2, userRole, true
        }
        if (!user2.authorities.contains(adminRole)) {
            UserRole.create user2, adminRole, true
        }

        assert User.count() == 2
        assert Role.count() == 2
        assert UserRole.count() == 3
		
		def program531 =new Program( programName: "531" ).save(failOnError: true)
				
		def lift1 = new ProgramLifts( lift: "Squat", program: program531).save(failOnError: true)
		def lift2 = new ProgramLifts( lift: "Bench", program: program531).save(failOnError: true)
		def lift3 = new ProgramLifts( lift: "Press", program: program531).save(failOnError: true)
		def lift4 = new ProgramLifts( lift: "Deadlift", program: program531).save(failOnError: true)
		
		
		
    }
		
    def destroy = {
    }
}
